package 二叉树;

public class q538二叉树转换累加树 {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    int pre = 0;

    public TreeNode convertBST(TreeNode root) {
        sum(root);
        return root;
    }
    void sum(TreeNode node){
        if(node==null) return ;
        sum(node.right);
        node.val += pre;
        pre = node.val;
        sum(node.left);
    }
}
